package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @Classname RequireCloseTimeHandler
 * @Description 要求关闭时间
 * @Author zhang.song
 * @Date 2022-03-31 15:30
 */
@Service
public class RequireCloseTimeHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        if(KiteStringUtils.isNotEmpty(commonAttr.getValue())){
            return;
        }

        LocalDateTime nowTime = LocalDateTime.now();
        nowTime = LocalDateTime.of(nowTime.getYear(), nowTime.getMonthValue(), nowTime.getDayOfMonth(), 23, 59, 59);
        nowTime = nowTime.plusDays(30);

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DateFormatUtils.getDateFormatTypeByAttrType(commonAttr.getAttrValueTypeId()));
        commonAttr.setValue(dtf.format(nowTime));
        setCommonAttr(commonAttr);
    }


    /**
     * 1.当前操作时间+30天 < 要求关闭时间
     * @author zhang.song
     * @date 2022-03-31 15:33
     */
    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        // 是否为页面初始化渲染，是（T），跳过校验
        String isInitPageRenderFlag = querySceneInstService.getExtParameterValue(sceneInstId, "isInitPageRenderFlag");
        if (KeyConsts.IFTRUE_T.equals(isInitPageRenderFlag)) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }

        if(KiteStringUtils.isNotEmpty(value) && value.length() > 9) {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DateFormatUtils.DATE_TIME_FORMAT.substring(0, value.length()));
            LocalDateTime localDateTime = null;
            if(value.length() == 10) {
                try {
                    LocalDate localDate = LocalDate.parse(value, dtf);
                    localDateTime = LocalDateTime.of(localDate, LocalTime.of(0, 0, 0));
                } catch (Exception e) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("日期转换异常");
                }
            } else {
                try {
                    localDateTime = LocalDateTime.parse(value, dtf);
                } catch (Exception e) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("日期转换异常");
                }
            }

            if(localDateTime != null) {
                LocalDateTime minDateTime = LocalDateTime.now().plusDays(30);
                if(localDateTime.isBefore(minDateTime)) {
                    BssException.throwOut("", "", "要求关闭时间不能小于当前时间+30天");
                }

                // 止租时间
                String rentStopTime = querySceneInstService.getCommonAttrValue(sceneInstId, "ORD10123");
                if(KiteStringUtils.isNotEmpty(rentStopTime) && rentStopTime.length() > 9) {
                    DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern(DateFormatUtils.DATE_TIME_FORMAT.substring(0, rentStopTime.length()));
                    LocalDateTime rentStopDateTime = null;
                    try {
                        rentStopDateTime = LocalDateTime.parse(rentStopTime, dtf2);
                    } catch (Exception e) {
                        ErrorConsts.BUSI_COMMON_ERROR.throwOut("日期转换异常");
                    }
                    if(rentStopDateTime != null) {
                        if(localDateTime.isAfter(rentStopDateTime)) {
                            BssException.throwOut("", "", "要求关闭时间不能大于止租时间");
                        }
                    }
                }
            }
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
